package set

/*
集合相关的操作
*/

type never struct{}

func From[E comparable](list ...E) map[E]never {
	res := map[E]never{}
	for _, e := range list {
		res[e] = never{}
	}
	return res
}

func Slice[E comparable](s map[E]never) []E {
	res := make([]E, 0, len(s))
	for e := range s {
		res = append(res, e)
	}
	return res
}

// Union 取并集
func Union[E comparable](s1, s2 map[E]never) map[E]never {
	res := map[E]never{}
	for e := range s1 {
		res[e] = never{}
	}
	for e := range s2 {
		res[e] = never{}
	}
	return res
}

// Intersect 取交集
func Intersect[E comparable](s1, s2 map[E]never) map[E]never {
	res := map[E]never{}
	for e := range s2 {
		if _, has := s1[e]; has {
			res[e] = never{}
		}
	}
	return res
}

// Subtract 去掉s1中也属于s2的元素
func Subtract[E comparable](s1, s2 map[E]never) map[E]never {
	res := map[E]never{}
	for e := range s1 {
		if _, has := s2[e]; !has {
			res[e] = never{}
		}
	}
	return res
}

// Contain 检查s1是否包含s2
func Contain[E comparable](s1, s2 map[E]never) bool {
	for e := range s2 {
		if _, has := s1[e]; !has {
			return false
		}
	}
	return true
}

// In 检查s1是否包含v
func In[E comparable](s map[E]never, e E) bool {
	_, has := s[e]
	return has
}

// Equal 两个切片的元素相同
func Equal[E comparable](s1, s2 map[E]never) bool {
	return Contain(s1, s2) && Contain(s2, s1)
}
